import { ipcMain } from 'electron';
import Todo from '../plugins/db/todo';
import Config from '../plugins/db/config';

const todo = new Todo();
const config = new Config();

export const initEvent = (app) => {
  ipcMain.handle('getTodo', () => todo.getAllTodo());
  ipcMain.handle('getConfig', () => config.get());
  ipcMain.on('set-app-config', (event, content) => {
    config.set(content.key, content.value);
  })
  ipcMain.on('set-auto', (event, content) => {
    // 是否开机自启动
    if (content) {
      app.setLoginItemSettings({
        openAtLogin: true,
    })} else {
      app.setLoginItemSettings({
        openAtLogin: false,
      });
    }
  })
  ipcMain.on('updateTodo', (event, data) => {
    todo.update(data);
  })
  ipcMain.on('setTodo', (event, item) => {
    todo.set(item.key, item.value);
  })
  ipcMain.on('deleteTodo', (event, key) => {
    todo.delete(key);
  })

   // 添加以下IPC处理
   ipcMain.on('vuex-set', (_, key, value) => {
    store.set(key, value)
  })

  ipcMain.on('vuex-remove', (_, key) => {
    store.delete(key)
  })

  ipcMain.handle('vuex-get', (_, key) => {
    return store.get(key)
  })

  // 在创建窗口后添加
  ipcMain.on('window-control', (_, action) => {
    const win = BrowserWindow.getFocusedWindow()
    if (!win) return

    switch(action) {
      case 'minimize':
        win.minimize()
        break
      case 'toggle-maximize':
        win.isMaximized() ? win.unmaximize() : win.maximize()
        break
      case 'close':
        win.close()
        break
    }
  })

  ipcMain.handle('window-get-state', () => {
    const win = BrowserWindow.getFocusedWindow()
    return win?.isMaximized() || false
  })

  ipcMain.on('exit-app', () => {
    mainWindow.close();
  })
};
